<?php
defined('IN_MAIN') or exit('Invalid Access!');

function calc_group_sales_print($uid, $date_from, $date_to, $which_field){
/*
This function will get total group sales of input uid (user ID) according to which network in the which_field.
*/

	global $vars;
	$db=$vars["db"];
	
	$calc_date['start'] = date("Y-m-d H:i:s", strtotime($date_from));
	$calc_date['end'] = date("Y-m-d H:i:s", strtotime($date_to));
	
	$upline_id = $uid;
	do{
		$t=@mysql_num_rows($r=mysql_query($s="select id from $db->users where $which_field in ($upline_id)"));
		$upline_id = '';
		if($t > 0){
			for($i=0;$i<$t;$i++){
				$downline = mysql_fetch_assoc($r);
				$upline_id .= $downline['id'].',';
				$all_downline_id .= $downline['id'].',';
			}
			$upline_id = rtrim($upline_id, ',');
		}
	}while($upline_id);
	
	$total_sales['bv'] = 0;
	$total_sales['dp'] = 0;
	$total_sales['rp'] = 0;
	if($all_downline_id){
		$all_downline_id = rtrim($all_downline_id, ',');
		$r=mysql_query($s="select sum(total_bv) as bv, sum(total_dp) as dp, sum(total_rp) as rp from $db->purchase where uid in ($all_downline_id) and first_sale='y' and no_bv!='y' and status='confirmed' and cdate>='$calc_date[start]' and cdate<='$calc_date[end]'");
		if(@mysql_num_rows($r)){
			$total_sales['bv'] = @mysql_result($r, 0, 'bv');
			$total_sales['dp'] = @mysql_result($r, 0, 'dp');
			$total_sales['rp'] = @mysql_result($r, 0, 'rp');
		}
	}
	
	return $total_sales;
}
?>